﻿using GYMm.UI.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace GYMm.UI.Views
{
	/// <summary>
	/// An empty page that can be used on its own or navigated to within a Frame.
	/// </summary>
	public sealed partial class DashboardView : Page
	{
		public DashboardView()
		{
			this.InitializeComponent();
		}

		private DashboardViewModel ViewModel()
		{
			return (DataContext as DashboardViewModel);
		}

		private void Grid_RightTapped(object sender, RightTappedRoutedEventArgs e)
		{
			
		}

		private async void OnRemoveClicked(object sender, RoutedEventArgs e)
		{
			await ViewModel().RemoveExerciseAsync();
		}

		private void OnItemClicked(object sender, ItemClickEventArgs e)
		{
			ViewModel().NavigateToExerciseDetails(e.ClickedItem as ExerciseItemViewModel);
		}

		private async void OnRemoveExerciseClick(object sender, RoutedEventArgs e)
		{
            NewExerciseBtn.Flyout.Hide();
			await ViewModel().RemoveExerciseAsync();
		}

		private async void OnNewClicked(object sender, RoutedEventArgs e)
		{
			await ViewModel().CreateNewExercise();
		}

		private async void OnSaveClick(object sender, RoutedEventArgs e)
		{
            NewExerciseBtn.Flyout.Hide();
			await ViewModel().UpdateExerciseDisplayNameAsync();
		}

		private void Grid_Loaded(object sender, RoutedEventArgs e)
		{
		}

		private void OnCloseClick(object sender, RoutedEventArgs e)
		{
            NewExerciseBtn.Flyout.Hide();
			ViewModel().HideExerciseEditDialog();
		}

		private void OnReorderClicked(object sender, RoutedEventArgs e)
		{
			if (MainGrid.ReorderMode == ListViewReorderMode.Disabled)
			{
				MainGrid.IsItemClickEnabled = false;
				MainGrid.ReorderMode = ListViewReorderMode.Enabled;
			}
			else
			{
				MainGrid.IsItemClickEnabled = true;
				MainGrid.ReorderMode = ListViewReorderMode.Disabled;
			}
		}

		private void Grid_Holding(object sender, HoldingRoutedEventArgs e)
		{
			ViewModel().ShowExerciseEditDialog((sender as Grid).DataContext as ExerciseItemViewModel);
            NewExerciseBtn.Flyout.ShowAt(this);
		}

        private void DialogBackTapped(object sender, TappedRoutedEventArgs e)
        {
            NewExerciseBtn.Flyout.Hide();
            ViewModel().HideExerciseEditDialog();
        }

        private async void OnBackupClick(object sender, RoutedEventArgs e)
        {
            await ViewModel().SendDbToEmail();
        }
	}
}
